package com.woniuxy.joylifeshop.controller;

import com.woniuxy.common.utils.JWTUtils;
import com.woniuxy.joylifeshop.cache.CartCache;
import com.woniuxy.joylifeshop.dto.ObjectDto;
import com.woniuxy.joylifeshop.exception.CartException;
import com.woniuxy.joylifeshop.service.CartService;
import com.woniuxy.joylifeshop.vo.ResponseInformation;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
public class CartController {

    @Autowired
    CartService cartService;

    @Autowired
    CartCache cartCache;

    @Autowired
    RabbitTemplate rabbitTemplate;

    /**
    * @Author SooYoo
    * @Description 删除一个购物车条目的方法
    * @Param
    * @Return
    **/
    @RequestMapping("/deleteCartItem")
    public ResponseInformation deleteCartItem(String cartId){
        ResponseInformation responseInformation = new ResponseInformation();
        //System.out.println(cartId);
        try {
            cartService.removeCartItem(cartId);
            responseInformation.setMsg("删除成功");
            responseInformation.setState(200);
            return responseInformation;
        } catch (Exception e) {
            e.printStackTrace();
            responseInformation.setState(500);
            responseInformation.setMsg("删除失败");
            return responseInformation;
        }
    }




    /**
    * @Author SooYoo
    * @Description 初始化一个用户的购物车
    * @Param
    * @Return
    **/
    @RequestMapping("/initCartPage")
    public ResponseInformation initCartPage(HttpServletRequest request){
        String userId = JWTUtils.getUserId(request.getHeader("token"));
        System.out.println(userId);
        //System.out.println(userId);
        ResponseInformation responseInformation = new ResponseInformation();
        try {
            List<ObjectDto> objectDtos = cartCache.readCartByUserIdCache(userId);
            responseInformation.setData(objectDtos);
            responseInformation.setMsg("查看成功");
            responseInformation.setState(200);

            return responseInformation;
        } catch (CartException e) {
            e.printStackTrace();
            responseInformation.setState(500);
            responseInformation.setMsg("fail");

            return responseInformation;
        }
    }



    /**
    * @Author SooYoo
    * @Description 初始化一个购物车条目,或者追加一个购物车条目
    * @Param
    * @Return
    **/
    @RequestMapping("/initCartItem")
    public ResponseInformation initCartItem(String shopId ,HttpServletRequest request){
        String userId = JWTUtils.getUserId(request.getHeader("token"));
        //测试代码
        //System.out.println(shopId);
        //System.out.println(userId);


        ResponseInformation responseInformation = new ResponseInformation();
        try {

            ObjectDto objectDto = new ObjectDto();
            objectDto.setData(shopId);
            objectDto.setData2(userId);

            //使用mq限流
            rabbitTemplate.convertAndSend("limit_exchange","",shopId+","+userId);

            responseInformation.setState(500);
            responseInformation.setMsg("成功添加购物车");

            return responseInformation;
        } catch (CartException e) {
            e.printStackTrace();
            responseInformation.setState(500);
            responseInformation.setMsg("添加购物车失败");

            return responseInformation;
        }


    }

}
